package com.app.miwawa.api.eam.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.framework.common.api.entity.CommonEntityUUID;
import org.framework.common.api.support.constant.SystemConstants;
import org.framework.common.api.support.model.ProcessModel;
import org.framework.common.ext.util.StringUtil;
import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonFormat;

/**
 * 班级记录表
 * <p>描述: 记录每日应到人数和负责老师</p>
 * <p>Company: Professional</p>
 * @author chanlong(陈龙)
 * @date 2016年2月3日 上午10:49:52
 * @version 1.0.2016
 */
@Entity @Table(name = "TB_EAM_CLASSES_RECORD")
public class ClassesRecord extends CommonEntityUUID {

	/** serialVersionUID(long):. */
	private static final long serialVersionUID = -8055223788797198965L;

	// 学员人数（应到人数）
	@Column(name = "students", nullable = true)
	private Integer students;
	
	// 班级ID
	@Column(name = "classes_id", length = 50, nullable = true)
	private String classesId;
	
	// 老师ID
	@Column(name = "teacher_id", length = 10, nullable = true)
	private String teacherId;
	
	// 签到日期
	@Temporal(TemporalType.DATE)
	@JsonFormat(pattern="yyyy-MM-dd", timezone="GMT+8")
	@DateTimeFormat(pattern="yyyy-MM-dd")
	@Column(name = "check_date", nullable = true)
	private Date checkDate;
	
	// 创建日期
	@Temporal(TemporalType.DATE)
	@JsonFormat(pattern="yyyy-MM-dd", timezone="GMT+8")
	@DateTimeFormat(pattern="yyyy-MM-dd")
	@Column(name = "create_date", nullable = true)
	private Date createDate;

	/**
	 * @see org.framework.common.api.entity.CommonEntityUUID#save(java.lang.String[])
	 * @author chanlong(陈龙)
	 * @date 2016年10月10日 下午5:29:17
	 */
	@Override
	public Object save(final String... options) throws Exception {
		ProcessModel process = ProcessModel.factory();
		if(!hasRecord(process) && StringUtil.isNotEmpty(this.teacherId)){
			List<Object> result = new ArrayList<>();
			String[] ids = this.teacherId.split(",");
			for (String tid : ids) {
				this.setId(null);
				this.setTeacherId(tid);
				
				result.add(process.bo.update(this));
			}
			return result;
		}else{
			return result(RETURN_INVALID, "当天已有应到设置记录！", "");
		}
	}
	
	/**
	 * @see org.framework.common.api.entity.CommonEntityUUID#save(java.util.Map)
	 * @author chanlong(陈龙)
	 * @date 2016年10月12日 下午1:16:53
	 */
	@Override
	public Object save(final Map<String, Object> params) throws Exception {
		return ProcessModel.factory().call.withProcedureName("pro_micui_eam_hascheck").execute("null", "null", this.classesId, this.checkDate);
	}

	/*
	 * 判断是否已有应到设置记录
	 */
	private boolean hasRecord(final ProcessModel process){
		String sql = process.properties.getProperty("classes.has.record");
		return process.jdbc.queryForObject(sql, Long.class, this.checkDate, this.classesId) > 0;
	}
	
	public ClassesRecord() {
		this.createDate = new Date();
		this.status = SystemConstants.DICT_STATUS_DEFAULT;
		this.invalid = SystemConstants.DICT_DELETE_INVALID;
	}
	
	public ClassesRecord(Date checkDate, String classesId) {
		this.checkDate = checkDate;
		this.classesId = classesId;
	}

	public Integer getStudents() {
		return students;
	}

	public void setStudents(Integer students) {
		this.students = students;
	}

	public String getClassesId() {
		return classesId;
	}

	public void setClassesId(String classesId) {
		this.classesId = classesId;
	}

	public String getTeacherId() {
		return teacherId;
	}

	public void setTeacherId(String teacherId) {
		this.teacherId = teacherId;
	}

	public Date getCheckDate() {
		return checkDate;
	}

	public void setCheckDate(Date checkDate) {
		this.checkDate = checkDate;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}
	
}
